$package('jeecg.TostHrSalaryinfo');

butcreate =function(callback){
	$('#create-data').dialog('open');
}

btnrece =function(callback){
	jeecg.confirm("警告！","重新生成，数据就将会在重新生成一遍，确认继续？",function(r){
		if(r==true){
			$('#recreate-data').dialog('open');
		}
	});
}
function probar(){
	document.getElementById("probar").style.display='block';
	var value = $('#probar').progressbar('getValue');
	if (value < 100){
		value += Math.floor(Math.random() * 10);
		$('#probar').progressbar('setValue', value);
	}
	if(value>=100){
		$('#probar').progressbar('setValue',0);
	}
	setTimeout(probar, 1000);
}

function probar2(){
	document.getElementById("probar2").style.display='block';
	var value = $('#probar2').progressbar('getValue');
	if (value < 100){
		value += Math.floor(Math.random() * 10);
		$('#probar2').progressbar('setValue', value);
	}
	if(value>=100){
		$('#probar2').progressbar('setValue',0);
	}
	setTimeout(probar2, 1000);
}

//全部生成
$(document).ready(function (){
	$("#btnok").click(function (){
		var month = $('#crmonth').datebox('getValue');
		if(month==null || month==''){
			alert("月份不能为空");
		}else{
			$('#create-data').dialog('close');
			$('#probarForm').dialog('open');
			probar();
			$.ajax({
				url: "createtab.do",
				dataType:'json',
				data:[{name:'month',value:month}],
				success:function (result){
					alert(result.msg);
					location.href="/myjhr/tostHrSalaryinfo/list.do";
				}
			});
		}
	});
});

//单个手动生成
$(document).ready(function (){
	$("#butregr").click(function (){
		$.messager.prompt("单个生成","请输入员工编号",function(id){
			if(id){
				var falg = "";
				$.ajax({
					url:"checkhrCode.do",
					dataType:'json',
					async:false,
					data:[{name:'hrCode',value:id}],
					success:function (result){
						falg = result;
					}
				});
				
				if(falg==false){
					$.messager.alert("错误","输入员工编号不存在！","error");
				}
				else if(falg==true){
				   var month = $('#crmonth').datebox('getValue');
				 if(month==null || month==''){
					alert("月份不能为空");
				  }else{
					$('#create-data').dialog('close');
					$('#probarForm').dialog('open');
						probar();
						$.ajax({
							url: "createtab.do",
							dataType:'json',
							data:[{name:'month',value:$('#crmonth').datebox('getValue')},
							      {name:'hrid',value:id}],
							success:function (result){
								alert(result.msg);
								location.href="/myjhr/tostHrSalaryinfo/list.do";
							}
						});
					}
				}else{
					$.messager.alert("错误","请输入正确格式的与员工编号","error");
				}
			}
		});
	});
});

function closeprobarForm(){
	$('#probarForm').dialog('close');
	$('#create-data').dialog('open');
}

jeecg.TostHrSalaryinfo = function(){
	var _box = null;
	var _this = {
		toList:function(parentId){
				_box.form.search.resetForm();
				if(parentId){
					$('#search_parentId').val(parentId);
					$('#btnback').linkbutton('enable');
					_box.grid.datagrid('hideColumn','childMenus');
				}else{
					$('#btnback').linkbutton('disable');
					$('#btnback').linkbutton('disable');
					_box.grid.datagrid('showColumn','childMenus');
				}
				_box.handler.refresh();
		},
		//设置默认按钮数据
		addDefBtns:function(){
			var defaultBtns= [
				
				{"btnName":"修改","menuid":2,"actionUrls":"getId.do|save.do","btnType":"edit"},
				{"btnName":"删除","menuid":2,"actionUrls":"delete.do","btnType":"remove"}
			];
			var tbline = $(".tb-line:visible");
			var btnType = $("input[name='btnType']",tbline);
			$.each(defaultBtns,function(i,btn){
				var isExists = false;
				//判断按钮类型是否存在
				if(btnType.length > 0){
					for(var i =0; i < btnType.length; i++){
						if(btnType.eq(i).val() == btn.btnType){
							isExists = true;
							break;
						}
					}
				}
				if(!isExists){
					_this.addLine(btn);
				}
			});
		},
		addLine: function(data){
			var table = $("#btn-tb");
			var	html = "<tr class='tb-line'>";
			html+=	   "	<td align='center'><span  class='newFlag red'>*</span></td>";
			html+=	   "	<td><input name=\"btnName\" class=\"easyui-validatebox text-name\" style=\"width:100%\" data-options=\"required:true\"></td>";
			html+=	   "	<td><input name=\"btnType\" class=\"easyui-validatebox text-name\" style=\"width:100%\" data-options=\"required:true\"></td>";
			html+=	   "	<td><input name=\"actionUrls\" class=\"easyui-validatebox text-desc\" style=\"width:100%\"  ></td>";
			html+=	   "	<td align='center'><a class=\"easyui-linkbutton remove-btn\"  iconCls=\"icon-remove\" plain=\"true\"></a>";
			html+=	   "	<input class=\"hidden\" name=\"btnId\">";
			html+=	   "	<input class=\"hidden\" name=\"deleteFlag\">";
			html+=	   "	</td>";
			html+=	   "</tr>";
			var line = $(html);
			//版定删除按钮事件
			$(".remove-btn",line).click(function(){
				jeecg.confirm('Confirm','Are you sure you want to delete record?',function(r){
					if(r){
						_this.delLine(line);
					}
				})
			});
			if(data){
				if(data.id){
					$(".newFlag",line).html(''); //清空新增标记
				}
				$("input[name='btnId']",line).val(data.id);
				$("input[name='btnName']",line).val(data.btnName);
				$("input[name='btnType']",line).val(data.btnType);
				$("input[name='actionUrls']",line).val(data.actionUrls);
			}
			$.parser.parse(line);//解析esayui标签
			table.append(line);
			
		},
		//删除全部
		delAllLine:function(b){
			if(b){
				$(".tb-line").remove();
			}else{
				$(".tb-line").each(function(i,line){
					_this.delLine($(line));
				});
			}
		},
		//删除单行
		delLine:function(line){
			if(line){
				var btnId = $("input[name='btnId']",line).val();
				if(btnId){
					$("input[name='deleteFlag']",line).val(1); //设置删除状态
					line.fadeOut();
				}else{
					line.fadeOut("fast",function(){
						 $(this).remove();
					});
				}
			}
		},
		config:{
  			action:{
  				save:'', //新增&修改 保存Action  
  				getId:'',//编辑获取的Action
  				delele:''//删除数据的Action
  			},
  			event:{
  				add : function(){
  					_this.delAllLine(true);//清空已有的数据
  					_box.handler.add();//调用add方法
					var parentId =$('#search_parentId').val();
					if(parentId){
						$("#edit_parentId").val(parentId);
					}
				},
				edit:function(){
					_this.delAllLine(true);
					_box.handler.edit(function(result){
					  if(result!=null && typeof(result.data.btns)!= "undefined"){
						$.each(result.data.btns,function(i,btn){
							_this.addLine(btn);
						});
					  }
					});
				}
  			},
  			dataGrid:{
  				title:'工资表信息',
	   			url:'dataList.do',
	   			columns:[[
	   			    {field:'id',title:'全选',checkbox:true},
	   			    {field:'hrCode',title:'员工编号',align:'center',width:70,sortable:true},	
	   			    {field:'hrName',title:'姓名',align:'center',width:50,sortable:true},	
					{field:'hsaMonth',title:'结算月份',align:'center',width:60,sortable:true},				
					{field:'hsaPushmoney',title:'提留部分',align:'right',width:60,sortable:true},				
					{field:'hsaPerformance',title:'绩效/提成',align:'right',width:60,sortable:true},				
					{field:'hsuId',title:'补贴',formatter:function(value,row,index){
						var html ="<a href=\""+urls['msUrl']+"tostHrSubsalaryinfo/list.do?id="+row.hsuId+"\">详情</a>";
						return html;
					},width:50,sortable:true},
					{field:'hsuAccount',title:'补贴合计',align:'right',width:70,sortable:true},				
					{field:'hfaId',title:'应发',formatter:function(value,row,index){
						var html ="<a href=\""+urls['msUrl']+"tostHrFactpayinfo/list.do?id="+row.hfaId+"\">详情</a>";
						return html;
					},width:50,sortable:true},				
					{field:'hfaCount',title:'应发合计',align:'right',width:70,sortable:true},				
					{field:'hdeId',formatter:function(value,row,index){
						var html ="<a href=\""+urls['msUrl']+"tostHrDeductinfo/list.do?id="+row.hdeId+"\">详情</a>";
						return html;
					},title:'扣款',width:50,sortable:true},				
					{field:'hdeDeductcount',title:'扣款金额',align:'right',width:60,sortable:true},				
					{field:'hsaPaid',title:'实发金额',align:'right',width:60,sortable:true},		
					{field:'hsaNum',title:'系数',width:30,sortable:true},			
					{field:'hsaDesc',title:'备注',width:70,sortable:true},
					/*{field:'hsaStatus',title:'工资状态',align:'center',width:60,sortable:true,formatter:function(value,row,index){
						if(value == 0){
							return "新生成";
						}
						if(value == 1){
							return "已结算";
						}
					}},*/
					{field:'createtime',title:'创建时间',width:120,sortable:true},
					{field:'person',title:'创建人',width:55,sortable:true}
				]],
				toolbar:[
					{id:'btnedit',text:'修改',btnType:'edit'},
					{id:'btndelete',text:'删除',btnType:'remove'},
					{id:'btncreate',text:'自动生成',btnType:'create',handler:butcreate},
					{
						id:'btnback',
						text:'back',
						disabled: true,
						iconCls:'icon-back',
						handler:function(){
							history.go(-1);
						}
					}
				]
			}
		},
		init:function(){
			_box = new YDataGrid(_this.config); 
			_box.init();
			$('#addLine_btn').click(_this.addLine);
			$('#addDefLine_btn').click(_this.addDefBtns);
			$('#delAllLine_btn').click(function(){
				jeecg.confirm('Confirm','Are you sure you want to delete record?',function(r){
					_this.delAllLine(false);
				});
			});
		}
	}
	return _this;
}();

$(function(){
	jeecg.TostHrSalaryinfo.init();
});		